package com.itump.base.util.web;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.itump.base.util.SpringUtils;

public class SpringApplicationContextHoldingListener implements ServletContextListener {

	public void contextInitialized(ServletContextEvent sce) {
		logger.info("initialzing spring application context holding servlet");
		ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
		SpringUtils.getApplicationContext();
	}
	
	public synchronized final static ApplicationContext getOnce(SpringUtils springUtils) {
		if (springUtils != null) {
			ApplicationContext ctx0 = ctx;
			ctx = null;
			return ctx0;
		}
		return null;
	}

	public void contextDestroyed(ServletContextEvent sce) {
	}
	
	private final static Log logger = LogFactory.getLog(SpringApplicationContextHoldingListener.class);
	
	private static ApplicationContext ctx;
	
}
